import { MongoClient, Collection } from "mongodb";
import { ISession, IUser } from "../models/user";
import { ITodo } from "../models/todo";
import { IProject } from "../models/project";
import { IKanban, IEpic } from "../models/project-detail";

export let sessions: Collection<ISession>;
export let users: Collection<IUser>;
export let projects: Collection<IProject>;
export let kanban: Collection<IKanban>;
export let epic: Collection<IEpic>;
export let todos: Collection<ITodo>;

export async function init() {
  const client = new MongoClient(process.env.MONGO_URL);
  await client.connect();
  const db = client.db();
  sessions = db.collection("sessions");
  users = db.collection("users");
  projects = db.collection("projects");
  kanban = db.collection("kanban");
  epic = db.collection("epic");
  todos = db.collection("todos");
  // 创建唯一索引
  users.createIndex(
    {
      account: 1,
    },
    {
      unique: true,
    }
  );
  users.createIndex(
    {
      nickname: 1,
    },
    {
      unique: true,
    }
  );
  // 为session创建ttl索引
  sessions.createIndex(
    {
      createdAt: 1,
    },
    {
      expireAfterSeconds: 60 * 60 * 24 * 1, // 过期时间, 这里暂时设置为1天
    }
  );
}
